import json

file = "generated_data.json"
with open(file, "r", encoding="utf-8") as f:
    data = json.load(f)
data_min_0 = None#无中转
price_min_0 = 10000
data_min_1_1 = None#1次第一躺
price_min_1_1 = 10000
data_min_1_2 = None#1次第二趟
price_min_1_2 = 10000

data_min_2_1 = None#2次第一躺
price_min_2_1 = 10000
data_min_2_2 = None#2次第二趟
price_min_2_2 = 10000
data_min_2_3 = None#2次第三躺
price_min_2_3 = 10000
start_input = input("请输入出发地：")
end_input = input("请输入目的地：")
#无中转
for item in data:
    start = item.get("start")
    end = item.get("end")
    time = item.get("time")
    available = item.get("available")
    price = item.get("price")
    #无中转
    if available == 1 and start == start_input and end == end_input:
        if price < price_min_0:
            price_min_0 = price
            data_min_0 = item
#中转一次1
for item in data:
    start = item.get("start")
    end = item.get("end")
    time = item.get("time")
    available = item.get("available")
    price = item.get("price")
    if available == 1 and start == start_input and time != 3:
        if price < price_min_1_1:
            price_min_1_1 = price
            data_min_1_1 = item
#中转一次2
for item in data:
    start = item.get("start")
    end = item.get("end")
    time = item.get("time")
    available = item.get("available")
    price = item.get("price")
    if available == 1 and end == end_input and start == data_min_1_1.get("end") and time != 3 and time != data_min_1_1.get("time"):
            if price < price_min_1_2:
                price_min_1_2 = price
                data_min_1_2 = item
#中转2次1
for item in data:
    start = item.get("start")
    end = item.get("end")
    time = item.get("time")
    available = item.get("available")
    price = item.get("price")
    if available == 1 and start == start_input and time == 1:
        if price < price_min_2_1:
            price_min_2_1 = price
            data_min_2_1 = item
#中转2次2
for item in data:
    start = item.get("start")
    end = item.get("end")
    time = item.get("time")
    available = item.get("available")
    price = item.get("price")
    if available == 1 and start == data_min_2_1.get("end") and time == 2:
            if price < price_min_2_2:
                price_min_2_2 = price
                data_min_2_2 = item
#中转2次3
for item in data:
    start = item.get("start")
    end = item.get("end")
    time = item.get("time")
    available = item.get("available")
    price = item.get("price")
    if available == 1 and end == end_input and start == data_min_2_2.get("end") and time == 3:
            if price < price_min_2_3:
                price_min_2_3 = price
                data_min_2_3 = item

if data_min_0['price'] > data_min_1_1['price']+data_min_1_2['price']:
    if data_min_1_1['price']+data_min_1_2['price'] > data_min_2_1['price']+data_min_2_2['price']+data_min_2_3['price']:
        print(f"中转2次最便宜的航班信息："
              f"{data_min_2_1['start']}->"
              f"{data_min_2_2['start']}->"
              f"{data_min_2_3['start']}->"
              f"{data_min_2_3['end']},"
              f"价格为{data_min_2_1['price']+data_min_2_2['price']+data_min_2_3['price']}")
    else:
        print(f"中转1次最便宜的航班信息："
              f"{data_min_1_1['start']}->"
              f"{data_min_1_2['start']}->"
              f"{data_min_1_2['end']},"
              f"价格为{data_min_1_1['price']+data_min_1_2['price']}")
else:
    print(f"直达最便宜的航班信息:"
          f"{data_min_0['start']}->"
          f"{data_min_0['end']},"
          f"价格为{data_min_0['price']}")



